package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.DomainService;
import com.szholly.pro.watergas_server.ps.service.OnlineMonitorService;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;
@Controller
@RequestMapping("/WasteGasOperationManage/*")
public class WasteGasOperationManageController extends BaseController{
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	DomainService domainService;
	
	@Autowired
	OnlineMonitorService onlineMonitorService;
	
	@RequestMapping(value = "/getMonitorParam")
	public void getMonitorParam(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String result = domainService.getDomainJSON("MONITOR_PARAM");
		outString(result);
	}

	@RequestMapping(value = "/getOperationJobs")
	public void getOperationJobs(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = DataUtil.CACHE+"select GUID,JOB_CODE,JOB_NAME from operation_job t where t.job_type='0' ";
		String result = DataUtil.getPageSelectJson(sql, null);
		outString(result);
	}

	@RequestMapping(value = "/getOperationJobParams")
	public void getOperationJobParams(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select t.s_domaincode,t.s_domaincaption from data_defulatdomain t where t.s_domainname='MONITOR_PARAM'";
		String result = DataUtil.getPageSelectJson(sql, null);
		outString(result);
	}
	
	@RequestMapping(value = "/getDataExceptions")
	public void getDataExceptions(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select t.s_domaincode,t.s_domaincaption from data_defulatdomain t where t.s_domainname='DATA_ANORMAL'  order by t.i_order";
		String result = DataUtil.getPageSelectJson(sql, null);
		outString(result);
	}

	@RequestMapping(value = "/getOperationJobCycles")
	public void getOperationJobCycles(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select  t.s_domaincode,t.s_domaincaption  from data_defulatdomain t where t.s_domainname='OPERATION_CYCLE_TIME'";
		String result = DataUtil.getPageSelectJson(sql, null);
		outString(result);
	}
	
	
	@RequestMapping(value = "/getOperationJobTolerances")
	public void getOperationJobTolerances(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select  t.s_domaincode,t.s_domaincaption  from data_defulatdomain t where t.s_domainname='OPERATION_TOLERANCE_TIME'";
		String result = DataUtil.getPageSelectJson(sql, null);
		outString(result);
	}
	//从字典表里查询行政区划信息
	@RequestMapping(value = "/getXZQH")
	public void getXZQH(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select  t.s_id,t.s_pid,t.s_domaincode,t.s_domaincaption  from data_defulatdomain t where t.s_domainname='政区' and t.I_JB<3 order by t.s_domaincode asc";
		String result = DataUtil.getPageSelectJson(sql, null);
		outString(result);
	}
	@RequestMapping(value = "/getProvinceAndCity")
	public void getProvinceAndCity(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select  t.s_id,t.s_pid,t.s_domaincode,t.s_domaincaption  from data_defulatdomain t where t.s_domainname='行政区划' and t.I_JB<3";
		String result = DataUtil.getPageSelectJson(sql, null);
		outString(result);
	}
	@RequestMapping(value = "/getStationType")
	public void getStationType(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select t.s_domaincode,t.s_domaincaption  from data_defulatdomain t where t.s_domainname='STATION_FLOW_NAME'";
		String result = DataUtil.getPageSelectJson(sql, null);
		outString(result);
	}
	
	@RequestMapping(value = "/getStationList")
	public void getStationList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String station_id_like = RequestUnit.getParameter("STATION_ID_LIKE");
		String flow_type = RequestUnit.getParameter("FLOW_TYPE");
		String water_type = RequestUnit.getParameter("WATER_TYPE");
		String build_status = RequestUnit.getParameter("BUILD_STATUS");
		
		Map<String,String> params = new HashMap<String,String>();
		if(StringUtil.IsNotNullOrEmpty(station_id_like)){
			params.put("station_id_like", station_id_like);
		}
		if(StringUtil.IsNotNullOrEmpty(flow_type)){
			params.put("flow_type", flow_type);
		}
		if(StringUtil.IsNotNullOrEmpty(water_type)){
			params.put("water_type", water_type);
		}
		if(StringUtil.IsNotNullOrEmpty(build_status)){
			params.put("build_status", build_status);
		}
		
		List<RowBase> list = onlineMonitorService.queryStation(params, null, null);
		String result = DataUtil.listToJSONObject(list).toString();
		
		outString(result);
	}
	
	@RequestMapping(value = "/getParamJobsList")
	public void getParamJobsList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String param = RequestUnit.getParameter("param");
		List<GlobalParameter> listParams = new ArrayList<GlobalParameter>();
		
		String sql = " select t.*,d1.s_domaincaption as paramname,d2.s_domaincaption as cyclename,d3.s_domaincaption as tolerancename,j1.job_name as JOB_NAME from OC_PARM_EVENT t"
				+" left join (select * from operation_job) j1 on j1.guid=t.job_id"
				+" left join (select s_domaincode,s_domaincaption from data_defulatdomain where s_domainname='MONITOR_PARAM') d1 on d1.s_domaincode=t.parm_id"
				+" left join (select s_domaincode,s_domaincaption from data_defulatdomain where s_domainname='OPERATION_CYCLE_TIME') d2 on d2.s_domaincode=t.default_cycle"
				+" left join (select s_domaincode,s_domaincaption from data_defulatdomain where s_domainname='OPERATION_TOLERANCE_TIME') d3 on d3.s_domaincode=t.default_tolerance where 1=1 ";
		
		if(StringUtil.IsNotNullOrEmpty(param)){
			sql+=" and t.parm_id = @param";
			listParams.add(new GlobalParameter("param",param));
		}
		
		String result = DataUtil.getPageSelectJson(sql, listParams);
		outString(result);
	}
	
	
	@RequestMapping(value = "/getGroups")
	public void getGroups(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String station = RequestUnit.getParameter("station");
		List<GlobalParameter> listParams = new ArrayList<GlobalParameter>();
		
		String sql = "select distinct fr.operation_group_id as GROUP_ID,fr.group_name from (select t.operation_group_id,g.group_name,t.station_id,m.name from operation_group_station_asso t "
				+" left join BC_SITE_INFO m on t.station_id=m.code"
				+" left join OC_GROUP g on t.operation_group_id=g.guid) fr where 1=1 ";
		
		if(StringUtil.IsNotNullOrEmpty(station)){
			sql+=" and fr.station_id = @station";
			listParams.add(new GlobalParameter("station",station));
		}
		
		sql += " order by fr.operation_group_id";
		String result = DataUtil.getPageSelectJson(sql, listParams);
		outString(result);
	}

	@RequestMapping(value = "/getOperationStationJobs")
	public void getOperationStationJobs(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String station = RequestUnit.getParameter("station");
		String jobs = RequestUnit.getParameter("jobs");
		String params = RequestUnit.getParameter("params");
		String cycles = RequestUnit.getParameter("cycles");
		String build_staus = RequestUnit.getParameter("BUILD_STATUS");
		String water_type = RequestUnit.getParameter("WATER_TYPE");
		
		List<GlobalParameter> listParams = new ArrayList<GlobalParameter>();
		
		String sql = "select * from (select t.GUID,t.job_id,t.parm_id,t.task_cycle,ms.name,ms.station_category,ms.build_status,j1.group_name,jb.job_name,d1.s_domaincaption as paramname,t.EFFECTIVE_TIME,"
				+"d2.s_domaincaption as cyclename, d3.s_domaincaption as tolerancename from OC_TASK_CONFIG t"
				+" left join (select t.station_id,g.group_name from operation_group_station_asso t left join OC_GROUP g on t.operation_group_id=g.guid) j1 on j1.station_id = t.station_id"
                +" left join (select s_domaincode, s_domaincaption from data_defulatdomain where s_domainname = 'MONITOR_PARAM') d1 on d1.s_domaincode = t.parm_id"
                +" left join (select s_domaincode, s_domaincaption from data_defulatdomain where s_domainname = 'OPERATION_CYCLE_TIME') d2 on d2.s_domaincode = t.task_cycle"
                +" left join (select s_domaincode, s_domaincaption from data_defulatdomain where s_domainname = 'OPERATION_TOLERANCE_TIME') d3 on d3.s_domaincode = t.task_tolerance"
				+" left join operation_job jb on t.job_id=jb.guid"
				+" left join BC_SITE_INFO ms on t.station_id = ms.code) rt where 1=1";
  
		if(StringUtil.IsNotNullOrEmpty(station)){
			sql+=" and rt.name like @station";
			listParams.add(new GlobalParameter("station","%"+station+"%"));
		}
		if(StringUtil.IsNotNullOrEmpty(jobs)){
			sql+=" and rt.job_id in "+jobs;
		}
		if(StringUtil.IsNotNullOrEmpty(params)){
			sql+=" and rt.parm_id in "+params;
		}
		if(StringUtil.IsNotNullOrEmpty(cycles)){
			sql+=" and rt.task_cycle in "+cycles;
		}
		//暂时屏蔽，站点事件关系设置不分供水、污水等类型
		if(StringUtil.IsNotNullOrEmpty(water_type)){
			sql+=" and rt.station_category = '"+water_type+"'";
		}
		if(StringUtil.IsNotNullOrEmpty(build_staus)){
			sql+=" and rt.build_status = '"+build_staus+"'";
		}
		
		String result = DataUtil.getPageSelectJson(sql, listParams);
		outString(result);
	}
	
	@RequestMapping(value = "/getParamJob")
	public void getParamJob(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String strSQL = " select job.*, dc1.s_domaincaption as PARAM_NAME, dc2.s_domaincaption as DEFAULT_CYCLE_TIME, dc3.s_domaincaption as DEFAULT_TOLERANCE_TIME from ("
				+" select t1.JOB_NAME, t1.JOB_CODE, t2.NAME, t2.JOB_ID, t2.PARM_ID, t2.DEFAULT_CYCLE, t2.DEFAULT_TOLERANCE, t2.GUID as PARAM_JOB_GUID from OPERATION_JOB t1, OC_PARM_EVENT t2 where t1.GUID = t2.JOB_ID ";
		String strJOB_NAME = RequestUnit.getString("JOB_NAME");
		String strNAME = RequestUnit.getString("NAME");
		String strJOB_ID = RequestUnit.getString("JOB_ID");
		String strPARM_ID = RequestUnit.getString("PARM_ID");
		
		if (strJOB_NAME != null && strJOB_NAME.length()>0)
			strSQL = strSQL + " and t1.JOB_NAME LIKE '%"+strJOB_NAME+"%' ";
		/*if (strJOB_CODE != null && strJOB_CODE.length()>0)
			strSQL = strSQL + " and t1.JOB_CODE = '"+strJOB_CODE+"' ";*/
		if (strNAME != null && strNAME.length()>0)
			strSQL = strSQL + " and t2.NAME LIKE '%"+strNAME+"%' ";
		if (strJOB_ID != null && strJOB_ID.length()>0)
			strSQL = strSQL + " and t2.JOB_ID = '"+strJOB_ID+"' ";
		if (strPARM_ID != null && strPARM_ID.length()>0)
			strSQL = strSQL + " and t2.PARM_ID = '"+strPARM_ID+"' ";
		
		strSQL = strSQL + " ) job left join DATA_DEFULATDOMAIN dc1 on (dc1.S_DOMAINNAME = 'MONITOR_PARAM' AND job.PARM_ID = dc1.S_DOMAINCODE) "
				+ " left join DATA_DEFULATDOMAIN dc2 on (dc2.S_DOMAINNAME = 'OPERATION_CYCLE_TIME' AND job.DEFAULT_CYCLE = dc2.S_DOMAINCODE) "
				+ " left join DATA_DEFULATDOMAIN dc3 on (dc3.S_DOMAINNAME = 'OPERATION_TOLERANCE_TIME' AND job.DEFAULT_TOLERANCE = dc3.S_DOMAINCODE) ";
				
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(strSQL, parm);
		outString(result);
	}
	
	@RequestMapping(value = "/updateParamJob")
	public void updateParamJob(HttpServletRequest resq, HttpServletResponse res) throws Exception {
	
		String NAME = RequestUnit.getString("NAME");
		String JOB_ID = RequestUnit.getString("JOB_ID");
		String strPARM_ID = RequestUnit.getString("JobParamList");
		String DEFAULT_CYCLE = RequestUnit.getString("DEFAULT_CYCLE");
		String DEFAULT_TOLERANCE = RequestUnit.getString("DEFAULT_TOLERANCE");

		try {
			DataUtil.beginTransaction();
			
			String deleteRelateSql = " DELETE from OC_PARM_EVENT WHERE JOB_ID=@JOB_ID ";
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			parms.add(new GlobalParameter("JOB_ID", JOB_ID));
			
			DataUtil.ExecuteUpdateSql(deleteRelateSql, parms);
			
			String[] PARM_IDs = strPARM_ID.split(",");
			for(int i=0;i<PARM_IDs.length;i++){
				String PARM_ID = PARM_IDs[i];
				String GUID = UUID.randomUUID().toString();
				if (GUID.length()<1)
					continue;
				String insertSql = " INSERT INTO OPERATION_PARM_JOB(GUID,JOB_ID,PARM_ID,NAME,DEFAULT_CYCLE,DEFAULT_TOLERANCE)VALUES(@GUID,@JOB_ID,@PARM_ID,@NAME,@DEFAULT_CYCLE,@DEFAULT_TOLERANCE) ";
				parms.clear();
				parms.add(new GlobalParameter("GUID", GUID));
				parms.add(new GlobalParameter("NAME", NAME));
				parms.add(new GlobalParameter("JOB_ID", JOB_ID));
				parms.add(new GlobalParameter("PARM_ID", PARM_ID));
				parms.add(new GlobalParameter("DEFAULT_CYCLE", DEFAULT_CYCLE));
				parms.add(new GlobalParameter("DEFAULT_TOLERANCE", DEFAULT_TOLERANCE));
				DataUtil.ExecuteUpdateSql(insertSql, parms);
			}
			DataUtil.commitTransaction();
			
		} catch (Exception e) {
			DataUtil.rollbackTransaction();
			throw e;
		}
	}
}
